package com.hrzyit.whistle.im.ui.adapter.holder

import android.os.Handler
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.hrzyit.base.utils.IMUtils
import com.hrzyit.whistle.im.R
import com.hrzyit.whistle.im.common.IMConstants
import com.hrzyit.whistle.im.entity.MessageInfo
import com.hrzyit.whistle.im.ui.adapter.ChatAdapter
import com.jude.easyrecyclerview.adapter.BaseViewHolder
import kotlinx.android.synthetic.main.item_chat_send.view.*

class ChatSendViewHolder(
    parent: ViewGroup, //    @Bind(R.id.chat_item_date)
    //    TextView chatItemDate;
    //    @Bind(R.id.chat_item_header)
    //    ImageView chatItemHeader;
    //    @Bind(R.id.chat_item_content_text)
    //    GifTextView chatItemContentText;
    //    @Bind(R.id.chat_item_content_image)
    //    BubbleImageView chatItemContentImage;
    //    @Bind(R.id.chat_item_fail)
    //    ImageView chatItemFail;
    //    @Bind(R.id.chat_item_progress)
    //    ProgressBar chatItemProgress;
    //    @Bind(R.id.chat_item_voice)
    //    ImageView chatItemVoice;
    //    @Bind(R.id.chat_item_layout_content)
    //    LinearLayout chatItemLayoutContent;
    //    @Bind(R.id.chat_item_voice_time)
    //    TextView chatItemVoiceTime;
    private val onItemClickListener: ChatAdapter.OnItemClickListener, private val handler: Handler
) : BaseViewHolder<MessageInfo>(parent, R.layout.item_chat_send) {

    override fun setData(data: MessageInfo?) {
        itemView.mSChatItemDateTv.text = if (data!!.time != null) data.time else ""

        Glide.with(context).load(data.header).into(itemView.mSChatItemHeaderIv)
        itemView.mSChatItemHeaderIv.setOnClickListener {
            onItemClickListener.onHeaderClick(dataPosition)
        }
        when {
            data.content != null -> {
                itemView.mSChatItemContentText.setSpanText(handler, data.content, true)
                itemView.mSChatItemVoiceIv.visibility = View.GONE
                itemView.mSChatItemContentText.visibility = View.VISIBLE
                itemView.mSChatItemLayoutContentLl.visibility = View.VISIBLE
                itemView.mSChatItemVoiceTimeTv.visibility = View.GONE
//                itemView.mSChatItemContentImage.visibility = View.GONE
            }
            data.imageUrl != null -> {
                itemView.mSChatItemVoiceIv.visibility = View.GONE
                itemView.mSChatItemLayoutContentLl.visibility = View.GONE
                itemView.mSChatItemVoiceTimeTv.visibility = View.GONE
                itemView.mSChatItemContentText.visibility = View.GONE
//                itemView.mSChatItemContentImage.visibility = View.VISIBLE
//                Glide.with(context).load(data.imageUrl).into(itemView.mSChatItemContentImage)
//                itemView.mSChatItemContentImage.setOnClickListener {
//                    onItemClickListener.onImageClick(
//                        itemView.mSChatItemContentImage,
//                        dataPosition
//                    )
//                }
            }
            data.filepath != null -> {
                itemView.mSChatItemVoiceIv.visibility = View.VISIBLE
                itemView.mSChatItemLayoutContentLl.visibility = View.VISIBLE
                itemView.mSChatItemContentText.visibility = View.GONE
                itemView.mSChatItemVoiceTimeTv.visibility = View.VISIBLE
//                itemView.mSChatItemContentImage.visibility = View.GONE
                itemView.mSChatItemVoiceTimeTv.text = IMUtils.formatTime(data.voiceTime)
                itemView.mSChatItemLayoutContentLl.setOnClickListener {
                    onItemClickListener.onVoiceClick(
                        itemView.mSChatItemVoiceIv,
                        dataPosition
                    )
                }
            }
        }
        when (data.sendState) {
            IMConstants.CHAT_ITEM_SENDING -> {
                itemView.mSChatItemProgress.visibility = View.VISIBLE
                itemView.mSChatItemFail.visibility = View.GONE
            }
            IMConstants.CHAT_ITEM_SEND_ERROR -> {
                itemView.mSChatItemProgress.visibility = View.GONE
                itemView.mSChatItemFail.visibility = View.VISIBLE
            }
            IMConstants.CHAT_ITEM_SEND_SUCCESS -> {
                itemView.mSChatItemProgress.visibility = View.GONE
                itemView.mSChatItemFail.visibility = View.GONE
            }
        }
    }
}
